package cn.piaoyi.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.Comment;
import cn.piaoyi.bean.Goods;
import cn.piaoyi.bean.UserInfo;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.CommentService;
import cn.piaoyi.service.UserInfoService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;
import cn.piaoyi.web.servlet.AdminSession;
import cn.piaoyi.web.ui.OptionsInt;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class AdminComment extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminComment.class);
	
	private CommentService commentService;

	public CommentService getCommentService() {
		return commentService;
	}

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}
	
	
	private long id;				//ID
	private int isShow = -1;		//认证状态
	private String reply; 
	private String message= "";

	

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	
	public int getIsShow() {
		return isShow;
	}

	public void setIsShow(int isShow) {
		this.isShow = isShow;
	}

	public String getReply() {
		return reply;
	}

	public void setReply(String reply) {
		this.reply = reply;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	private PageList pageList;
	private Comment comment;
	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}

	public Comment getComment() {
		return comment;
	}

	public void setComment(Comment comment) {
		this.comment = comment;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/admin_comment.action");
		this.setPageList(this.getCommentService().findComments(-1,-1, isShow,"id", 1, pages));
		return SUCCESS;
	}

	public String reply(){
		this.setAction("doreply");
		this.message = "";
		this.setComment(this.getCommentService().findCommentById(this.id));
		this.setReply(this.comment.getReply());
		return "reply";
	}
	
	public String doreply(){
		//添加回复
		this.comment = this.getCommentService().findCommentById(this.id);
		this.comment.setReply(this.reply);
		this.comment.setReplyTime(new Date());
		try {
			this.getCommentService().updateComment(this.comment);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		this.message = "ok";
		return "reply";
	}

}
